<?
require "./admin/config.php";
require "./admin/class/mysql.php";
require "./include/enetoptions.php";
require "./admin/adminfunctions.php";
require "./include/forms.php";
require "./include/pagenav.php";
require "./admin/class/upload.php";
require "./include/imgcontrol.php";
require "./include/php-captcha.inc.php";

$cpforms = new FORMS;

if (get_magic_quotes_gpc()) { 
	function stripslashes_deep($value) 
	{ 
		$value = is_array($value) ? 
		array_map('stripslashes_deep', $value) : 
		stripslashes($value); 

		return $value; 
	} 

	$__POST = array_map('stripslashes_deep', $_POST); 
	$__GET = array_map('stripslashes_deep', $_GET); 
	$__COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 


define('SMARTY_DIR', './include/smarty/');
// hack version example that works on both *nix and windows
// Smarty is assumend to be in 'includes/' dir under current script
require_once(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty();

$smarty->template_dir = './templates/';
$smarty->compile_dir  = './templates_c/';
$smarty->config_dir   = './configs/';
$smarty->cache_dir    = './cache/';
$smarty = new Smarty;
$charset = "utf-8";

$DB = new DB_MySQL;

$DB->servername=$servername;
$DB->dbname=$dbname;
$DB->dbusername=$dbusername;
$DB->dbpassword=$dbpassword;
$DB->mysqlver=$mysqlver;
$DB->dbcharset=$dbcharset;
$DB->connect();
$DB->selectdb();
$DB->query("SET NAMES 'utf8'"); 

@session_start();
header("Cache-control: private");

function ReplaceSql_Str($str_temp)
{
	$str_temp = str_replace("\'","",$str_temp);
	$str_temp = str_replace("\\","",$str_temp);
	$str_temp = str_replace("\"","",$str_temp);
	return $str_temp;
}

$smarty->assign("uname",$_SESSION['uname']);
$smarty->assign("uid",$_SESSION['uid']);
$smarty->assign("utype",$_SESSION['usertype']);
$smarty->assign("u_url",$_SESSION['homeUrl']);
$smarty->assign("u_status",$_SESSION['status']);
$smarty->assign("footer_year",date("Y"));

function  pageshow($linkfile,$offset,$totals,$other)
{
		if($totals<1) $totals=1;
		$pages = ceil($totals/2);
		if($pages>100) $pages=100;
		if($offset>$pages) $offset=$pages;
				
		if($pages==0)	$pages=1;

		$page  = $offset;
		
		$rs = '';
	$rs .= '<table boder="0" width="100%" cellpadding="4" cellspacing="1" class="sp"><tr><td>';
	if($page-1>=1)
	{
		$t_page=$page-1;
		$rs .='<span><a href="'.$linkfile.'?offset='.$t_page.$other.'">'.'Perv'.'</a></span>';
	}
	if($page>=5)
	{
		$rs .='<span class="sp1"><a href="'.$linkfile.'?offset='.'1'.$other.'">'.'1'.'</a></span>';
		$rs .='<span class="sp1"><a href="'.$linkfile.'?offset='.'2'.$other.'">'.'2'.'</a></span>';
		$rs .='...';
	}
	if($page==4)
	{
		$rs .='<span class="sp1"><a href="'.$linkfile.'?offset='.'1'.$other.'">'.'1'.'</a></span>';
	}
	for($k=$page-2;$k<$page+3;$k++)
	{
		if($k>0&&$k<=$pages&&$k!=$page)
			$rs .='<span class="sp1"><a href="'.$linkfile.'?offset='.$k.$other.'">'.$k.'</a></span>';
		if($k==$page)
			$rs .='<span class="sp1 rd">'.$k.'</span>';
	}

	if($page<=$pages-4)
	{
		$rs .='...';
		for($i=$pages-1;$i<=$pages;$i++)
			$rs .='<span class="sp1"><a href="'.$linkfile.'?offset='.$i.$other.'">'.$i.'</a></span>';
	}
	if($page==$pages-3)
	{
		$rs .='<span class="sp1"><a href="'.$linkfile.'?offset='.$pages.$other.'">'.$pages.'</a></span>';
	}
	if($page+1<=$pages)
	{
		$t_page=$page+1;
		$rs .='<span><a href="'.$linkfile.'?offset='.$t_page.$other.'">'.'Next'.'</a></span>';
	}
	$rs .="</td><td align=right>Total:".$totals." </td></tr></table>";
	return $rs;

}

function checkFormSubmit($iTimeOffset=3600){
    // 取得表单的标识
    $idForm = md5(serialize($_POST));
    // 是否需要表单提交检察
    $iFormCheck = true;
    if (isset($_SESSION['formSubmitCheck'])){
        // 删除过期的表单标识
        foreach (array_keys($_SESSION['formSubmitCheck']) as $val){
            if (time() > $val){
                unset($_SESSION['formSubmitCheck'][$val]);
            }
        }
    }else {
        $_SESSION['formSubmitCheck'] = array();
        $iFormCheck = false;
    }
    if ($iFormCheck == true){
        // 检查是否有重复标识的提交记录
        foreach ($_SESSION['formSubmitCheck'] as $val){
            if ($val == $idForm){
                return false;
            }
        }
    }
    // 保存表单标识
    $_SESSION['formSubmitCheck'][(time()+$iTimeOffset)] = $idForm;
    return true;
}


//deadline
$deadline_conf = "7776000";
$deadline_conf_month = "2592000";
$oneyear_conf  = "31536000";


//getNumberOfApplicants

function getNumberOfApplicants($jobid,$own=''){
   global $DB,$_SESSION;
   if($own == 1){
	   $owner  = "  AND a.app_recruitid ='".$_SESSION[uid]."' ";
       $owner .= "  AND (a.app_isdelete = '1' OR  a.app_isdelete = '0')";
	   $sql = "SELECT COUNT(*) AS count FROM pa_applicant as a ,pa_resume as b,pa_recuiter as c,pa_job as d  WHERE  d.jo_id = a.app_jobid AND a.app_resumeid = b.re_id AND c.rc_id = a.app_recruitid  AND a.app_jobid = '".$jobid."' $owner";

   }else{
	   $owner  = "  AND (a.app_isdelete = '2' OR  a.app_isdelete = '0')";
	   $sql = "SELECT COUNT(*) AS count FROM pa_applicant as a ,pa_resume as b,pa_job as d  WHERE  d.jo_id = a.app_jobid AND a.app_resumeid = b.re_id  AND a.app_jobid = '".$jobid."' $owner";
   }
   $arr = $DB->fetch_one_array($sql);
   return $arr['count'];
}

function getNumberOfContacted($jobid,$own=''){
   global $DB,$_SESSION;
   if($own == 1){
	   $owner  = "  AND re_uid ='".$_SESSION[uid]."' ";
	   $sql = "SELECT COUNT(*) AS count FROM pa_contact WHERE  re_aliasid = '".$jobid."' $owner";

   }else{
	   $owner  = "  AND b.jo_uid = '".$_SESSION[uid]."'";
	   $sql = "SELECT COUNT(*) AS count FROM pa_contact as a ,pa_job as b  WHERE  b.jo_id = a.re_aliasid AND a.re_aliasid = '".$jobid."' $owner";
   }
   $arr = $DB->fetch_one_array($sql);
   return $arr['count'];
}

function getNumberOfApproved($jobid,$own=''){
   global $DB,$_SESSION;
   if($own == 1){
	  // $owner  = "  AND re_uid ='".$_SESSION[uid]."' ";
	  // $sql = "SELECT COUNT(*) AS count FROM pa_contact WHERE  re_aliasid = '".$jobid."' $owner";

   }else{
	   $owner  = "  AND b.jo_uid = '".$_SESSION[uid]."' AND b.jo_id = '".$jobid."' ";
	   $sql = "SELECT COUNT(*) AS count FROM pa_contact as a ,pa_job as b  WHERE  b.jo_id = a.re_aliasid AND a.re_isapproved = '1' $owner";
   }
   $arr = $DB->fetch_one_array($sql);
   return $arr['count'];
}

function areFriends($uid, $fid){
	global $DB;
	if($uid <1 || $fid <1) return false;
	$a_result = $DB->fetch_one_array("SELECT count(*) as count FROM pa_mycontacts WHERE myc_rcid = '".$uid."' AND myc_fid = '".$fid."' ");
	$b_result = $DB->fetch_one_array("SELECT count(*) as count FROM pa_mycontacts WHERE myc_fid = '".$uid."' AND myc_rcid = '".$fid."' ");

	if($a_result['count']>0 && $b_result['count']>0){
		return true;
	}	
	return false;
}

function friendList($uid){
	global $DB;
	if($uid <1) return false;
	$result = $DB->fetch_all("SELECT myc_fid FROM pa_mycontacts WHERE myc_rcid = '".$uid."' ");
	if(!empty($result)){
		foreach($result as $v){
			if($v[myc_fid] == 0) continue;
			$ids[] = $v[myc_fid];
		}
	}
	return $ids;
}

function isSentRequest($senderId, $receiverId){
	global $DB;
	//e_isreplied	
	$sql = "SELECT count(*) as count FROM pa_email WHERE e_sentrcid = '".$senderId."' AND e_receivedrcid = '".$receiverId."' AND e_isreplied = '0' AND e_type='3' ";
	$result = $DB->fetch_one_array($sql);
	if($result['count'] > 0 ){
		return true;
	}
	return false;
}


function changeURL($url){
   if(!preg_match("/^http:\/\//i",$url)){
     $url  = "http://".$url; 
   }
   return $url;
}
function cutURL($url){
   $url = @str_replace("http://","",$url);
   $url = @str_replace("https://","",$url);
   return $url;
}

function myf($l){
if(!ereg("^[0-9]+$",$l) || $l>1000) return;
for($c='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',$r='';$l>0;$l--,$r.=$c[rand(0,35)]);
return $r;
}


function getStatusOfinvite($jid,$rcid,$reid){
	global $DB;
   if($jid != ''){
	   $sqljobid = " in_jobid = '".$jid."' AND ";
   }
   $sql = "SELECT * FROM pa_invite WHERE ".$sqljobid." in_recruiterid = '".$rcid."' AND in_resumeid = '".$reid."' ";
   //echo $sql;
   $rs  = $DB->fetch_one_array($sql);
   if(empty($rs)){
     return false;
   }else{
     return $rs[in_status];
   }
}


function getJobDetail($jid){
	global $DB;
	$sql = "SELECT * FROM pa_job WHERE jo_id = '".$jid."'";
	$rs  = $DB->fetch_one_array($sql);
    return $rs;
}

function getRecruiterDetail($rid){
	global $DB;
	$sql = "SELECT * FROM pa_recuiter WHERE rc_id = '".$rid."'";
	$rs  = $DB->fetch_one_array($sql);
    return $rs;
}

function getResumeDetail($reid){
	global $DB;
	$sql = "SELECT * FROM pa_resume WHERE re_id = '".$reid."'";
	$rs  = $DB->fetch_one_array($sql);
    return $rs;
}

//发送邮件验证
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) {  

    $ckey_length = 4;  
    // 随机密钥长度 取值 0-32;  
    // 加入随机密钥，可以令密文无任何规律，即便是原文和密钥完全相同，加密结果也会每次不同，增大破解难度。  
    // 取值越大，密文变动规律越大，密文变化 = 16 的 $ckey_length 次方  
    // 当此值为 0 时，则不产生随机密钥  
      
  
    $key = md5 ( $key ? $key : 'key' ); //这里可以填写默认key值  
    $keya = md5 ( substr ( $key, 0, 16 ) );  
    $keyb = md5 ( substr ( $key, 16, 16 ) );  
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';  
      
    $cryptkey = $keya . md5 ( $keya . $keyc );  
    $key_length = strlen ( $cryptkey );  
      
    $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;  
    $string_length = strlen ( $string );  
      
    $result = '';  
    $box = range ( 0, 255 );  
      
    $rndkey = array ();  
    for($i = 0; $i <= 255; $i ++) {  
        $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );  
    }  
      
    for($j = $i = 0; $i < 256; $i ++) {  
        $j = ($j + $box [$i] + $rndkey [$i]) % 256;  
        $tmp = $box [$i];  
        $box [$i] = $box [$j];  
        $box [$j] = $tmp;  
    }  
      
    for($a = $j = $i = 0; $i < $string_length; $i ++) {  
        $a = ($a + 1) % 256;  
        $j = ($j + $box [$a]) % 256;  
        $tmp = $box [$a];  
        $box [$a] = $box [$j];  
        $box [$j] = $tmp;  
        $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );  
    }  
      
    if ($operation == 'DECODE') {  
        if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {  
            return substr ( $result, 26 );  
        } else {  
            return '';  
        }  
    } else {  
        return $keyc . str_replace ( '=', '', base64_encode ( $result ) );  
    }  
  
}  


function accessblock($arr){
	global $_SESSION;
	if(in_array($_SESSION['status'],$arr)){
		redirect2("errorall.php?errid=rc_1016");
		exit;
	}
}
?>